/**
 * 贵州省市县采购网
 */
import { Base, Item } from '../base';
class gzssxcg extends Base {
    constructor() {
        super();
        this.name = "贵州省市县采购网";
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "ztb";
        this.collectionShowName = "招投标信息";
        this.keyField = "title";
    }
    getItems(pageIndex: number): Item[] {
        let num = pageIndex + 1;
        let result:Item[] = [];
        let that = this;
        let passParameter={};
        try {
            if (num == 1) {
                let url = `http://www.ccgp-guizhou.gov.cn/article-search.html`;
                let content = this.spider.getSync(url);
                let $ = this.cheerio.load(content);
                (<any>passParameter).$=$;
                (<any>passParameter).that=that;
                (<any>passParameter).result=result;
                result = this.getData(passParameter);
            } else {
                let urlPost = `http://www.ccgp-guizhou.gov.cn/article-search.html`;
                let postData = {
                    "articlePageNo": num,
                    "articlePageSize":15,
                    "category.id":1153418052184995
                }
                let content = this.spider.postSync(urlPost, postData);
                let $ = this.cheerio.load(content);
                (<any>passParameter).$=$;
                (<any>passParameter).that=that;
                (<any>passParameter).result=result;
                result = this.getData(passParameter);
            }
        }catch (error) {
            console.log(error);
        }
        return result;
    }
    getData(passParameter:any) {
        passParameter.$('div.xnrx ul li').each(function (index:any, ele:any) {
            let item = passParameter.that.buildItem();
            item.title = passParameter.$(ele).find('a').text().trim();
            item['采购日期'] = passParameter.$(ele).find('span').text().trim();
            item.source = passParameter.that.name;
            let uri = 'http://www.ccgp-guizhou.gov.cn' + passParameter.$(ele).find('a').attr('href');
            item.url = 'http://www.ccgp-guizhou.gov.cn' + passParameter.$(ele).find('a').attr('href');
            passParameter.result = passParameter.that.getExtraItem(uri, item, passParameter.result);
        });
        return passParameter.result;
    }
    getExtraItem(url: string, item: Item, result: Item[]) {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content, { decodeEntities: false });
        (<any>item).content = $('.you').html();
        result.push(item);
        return result;
    }
    getPageCount() {
        return 20;
    }
}
module.exports = new gzssxcg();